package com.polaris.live.ui.live.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.WidgetLiveLevelBinding
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.RichCharmImgUtil

/**
 * LiveLevelView
 *
 * @author Created by lucas on 2023/11/17 16:09
 */
class LiveLevelView(context: Context, attrs: AttributeSet? = null) : RelativeLayout(context, attrs) {

    private val mBinding = WidgetLiveLevelBinding.inflate(LayoutInflater.from(context), this)

    fun init(attrs: AttributeSet?) {
        if (attrs != null) {
            val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.LiveLevelView, 0, 0)
            val imgHeight = a.getDimension(R.styleable.LiveLevelView_img_height, 16.autoDp.toFloat())
            val imgWidth = a.getDimension(R.styleable.LiveLevelView_img_width, 16.autoDp.toFloat())
            val tvSize = a.getInt(R.styleable.LiveLevelView_tv_size, 11)
            a.recycle()

            val lp = mBinding.levelImg.layoutParams as MarginLayoutParams
            lp.width = imgWidth.toInt()
            lp.height = imgHeight.toInt()

            mBinding.levelImg.layoutParams = lp
            mBinding.tvLevel.textSize = tvSize.toFloat()
        }
    }

    fun setData(sex: Int, richs: Long?, charms: Long?) {
        mBinding.apply {
            val isMan = CommonUtils.isMan(sex)
            val level = if (isMan) {
                RichCharmImgUtil.getLiveRichImg(richs ?: 0)
            } else {
                RichCharmImgUtil.getLiveCharmImg(charms ?: 0)
            }

            val image = level.second
            if (image == null) {
                visibility = GONE
            } else {
                levelImg.setImageResource(image)
                tvLevel.text = "${level.first}"

                tvLevel.textColorBuilder.textStrokeColor = ColorUtils.getColor(level.third)
                tvLevel.textColorBuilder.intoTextColor()
            }
        }
    }
}